import React from 'react'
import { Table, Tooltip, Progress } from 'antd'
import _ from 'lodash'
import { FormattedMessage } from 'react-intl'
import { convertContent } from '../../../../../constant/lang-util'

export default class AssignmentsStatusBox extends React.Component {

  getColumns = () => {
    return [{
      title: <FormattedMessage id='作业'/>,
      dataIndex: 'title'
    }, {
      title: <FormattedMessage id='总人数'/>,
      dataIndex: 'total'
    }, {
      title: <FormattedMessage id='未做人数'/>,
      dataIndex: 'unSubmitted'
    }, {
      title: <FormattedMessage id='已提交人数'/>,
      dataIndex: 'submitted'
    }, {
      title: <FormattedMessage id='未完成人数'/>,
      dataIndex: 'unFinished',
      render: (text, record) => {
        return <Tooltip title={convertContent('分数低于60分')}>
          <a onClick={() => this.props.onChange(record.id)}>{text}</a>
        </Tooltip>
      }
    }, {
      title: <FormattedMessage id='已完成人数'/>,
      dataIndex: 'finished',
      render: (text, record) => {
        return <Tooltip title={convertContent('分数高于60分')}>
          <a onClick={() => this.props.onChange(record.id)}>{text}</a>
        </Tooltip>
      }
    }, {
      title: <FormattedMessage id='已评阅人数'/>,
      dataIndex: 'reviewed'
    }, {
      title: <FormattedMessage id='提交率'/>,
      dataIndex: 'submittedRate',
      width: 200,
      render: text => <Progress
        percent={parseInt(text * 100)}
      />
    }, {
      title: <FormattedMessage id='完成率'/>,
      dataIndex: 'finishRate',
      width: 200,
      render: text => <Progress
        percent={parseInt(text * 100)}
      />
    }, {
      title: <FormattedMessage id='评阅率'/>,
      dataIndex: 'reviewRate',
      width: 200,
      render: text => <Progress
        percent={parseInt(text * 100)}
      />
    }]
  }
  statistic = (currentAssignment, id) => {
    const title = currentAssignment[0]['任务卡'] + ' / ' + currentAssignment[0]['作业']
    const total = currentAssignment.length
    const submitted = currentAssignment.filter(item => item['状态'] === '已提交').length
    const unSubmitted = currentAssignment.filter(item => item['状态'] === '未提交').length
    const unFinished = currentAssignment.filter(item => item['状态'] === '已完成' && item['分数'] <= 60).length
    const finished = currentAssignment.filter(item => item['状态'] === '优秀' || item['分数'] > 60).length
    const reviewed = currentAssignment.filter(item => item['已评阅'] > 0).length
    const submittedRate = ((total-unSubmitted) / total).toFixed(2)
    const finishRate = (finished / total).toFixed(2)
    const reviewRate = (reviewed / total).toFixed(2)

    return {id, title, total, unFinished, unSubmitted, submitted, finished, reviewed,submittedRate, finishRate, reviewRate}
  }
  getData = assignmentsQuizzesStatus => {
    const result = []
    const data = _.groupBy(assignmentsQuizzesStatus, item => item['id'])
    const ids = [...new Set(assignmentsQuizzesStatus.map(item => item.id))]
    ids.forEach(key => {
      const currentAssignment = data[key]
      result.push(this.statistic(currentAssignment, key))
    })

    return result
  }

  render () {
    const {assignmentsQuizzesStatus} = this.props
    const columns = this.getColumns(assignmentsQuizzesStatus)
    const data = this.getData(assignmentsQuizzesStatus)
    return <Table columns={columns}
                  dataSource={data}
                  pagination={false}
                  bordered
                  size='small'
    />
  }
}
